﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace WebLib.CryptoService
{
    public class MD5Helper
    {
        public static string GetMd5Hash(string input)
        {
            var encodingBytes = Encoding.UTF8.GetBytes(input);
            var hashedBytes = MD5.Create().ComputeHash(encodingBytes);

            var sBuilder = new StringBuilder();

            foreach (var b in hashedBytes)
            {
                sBuilder.Append(b.ToString("x2"));
            }

            return sBuilder.ToString();
        }

        public static bool VerifyMd5Hash(string input, string hash)
        {
            // Hash the input.
            var hashOfInput = GetMd5Hash(input);

            // Create a StringComparer an compare the hashes.
            var comparer = StringComparer.OrdinalIgnoreCase;

            return 0 == comparer.Compare(hashOfInput, hash);
        }
    }
}
